<?
    require_once("mvc/action/ActionForm.php");
    require_once("mvc/action/ActionMessage.php");
    require_once("mvc/action/ActionMessages.php");

    require_once("cms/staff/om/Staff.php");


    class BaseStaffForm extends ActionForm {

        protected $staff;
        protected $password2;
        protected $roles;
        protected $formImage;
        protected $deleteImage;

        public function __construct() {
            $this->staff = new Staff();
            $this->password2 = null;
            $this->roles = array();
            $this->formImage = null;
            $this->deleteImage = false;
        }

        public function getStaff() {
            return $this->staff;
        }

        public function setStaff(Staff $staff) {
            $this->staff = $staff;
        }

        public function getPassword2() {
            return $this->password2;
        }

        public function setPassword2($password2) {
            $this->password2 = $password2;
        }

        public function getRoles() {
            return $this->roles;
        }

        public function setRoles($roles) {
            $this->roles = $roles;
        }

        public function getFormImage() {
            return $this->formImage;
        }

        public function setFormImage($formImage) {
            $this->formImage = $formImage;
        }

        public function getDeleteImage() {
            return $this->deleteImage;
        }

        public function setDeleteImage($deleteImage) {
            $this->deleteImage = $deleteImage;
        }

        public function validate() {

            $errors = new ActionMessages();

            if(is_null($this->getStaff()->getFirstname()) || strlen($this->getStaff()->getFirstname()) == 0)
                $errors->add("staff->firstname",
                    new ActionMessage("errors.required", true, new ActionMessage("prompt.firstname", true)));


            if(!is_null($this->getStaff()->getFirstname()) && strlen($this->getStaff()->getFirstname()) > 0 && strlen($this->getStaff()->getFirstname()) > 255)
                $errors->add("staff->firstname",
                    new ActionMessage("errors.maxlength", true, array(
                        new ActionMessage("prompt.firstname", true),
                        new ActionMessage("255", false))));


            if(is_null($this->getStaff()->getSurname()) || strlen($this->getStaff()->getSurname()) == 0)
                $errors->add("staff->surname",
                    new ActionMessage("errors.required", true, new ActionMessage("prompt.surname", true)));


            if(!is_null($this->getStaff()->getSurname()) && strlen($this->getStaff()->getSurname()) > 0 && strlen($this->getStaff()->getSurname()) > 255)
                $errors->add("staff->surname",
                    new ActionMessage("errors.maxlength", true, array(
                        new ActionMessage("prompt.surname", true),
                        new ActionMessage("255", false))));


            if(is_null($this->getStaff()->getUsername()) || strlen($this->getStaff()->getUsername()) == 0)
                $errors->add("staff->username",
                    new ActionMessage("errors.required", true, new ActionMessage("prompt.username", true)));


            if(!is_null($this->getStaff()->getUsername()) && strlen($this->getStaff()->getUsername()) > 0 && strlen($this->getStaff()->getUsername()) > 255)
                $errors->add("staff->username",
                    new ActionMessage("errors.maxlength", true, array(
                        new ActionMessage("prompt.username", true),
                        new ActionMessage("255", false))));


            if(!is_null($this->getStaff()->getUsername()) && strlen($this->getStaff()->getUsername()) > 0 && !ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $this->getStaff()->getUsername()))
                $errors->add("staff->username",
                    new ActionMessage("errors.email", true, new ActionMessage("prompt.username", true)));


            if(!is_null($this->getStaff()->getPassword()) && strlen($this->getStaff()->getPassword()) > 0 && strlen($this->getStaff()->getPassword()) < 5)
                $errors->add("staff->password",
                    new ActionMessage("errors.minlength", true, array(
                        new ActionMessage("prompt.password", true),
                        new ActionMessage("5", false))));

            if(!is_null($this->getStaff()->getPassword()) && strlen($this->getStaff()->getPassword()) > 0 && strlen($this->getStaff()->getPassword()) > 255)
                $errors->add("staff->password",
                    new ActionMessage("errors.maxlength", true, array(
                        new ActionMessage("prompt.password", true),
                        new ActionMessage("255", false))));


            if(is_null($this->getStaff()->getLanguageCode()) || strlen($this->getStaff()->getLanguageCode()) == 0)
                $errors->add("staff->languageCode",
                    new ActionMessage("errors.required", true, new ActionMessage("prompt.language", true)));


            if(!is_null($this->getStaff()->getLanguageCode()) && strlen($this->getStaff()->getLanguageCode()) > 0 && strlen($this->getStaff()->getLanguageCode()) > 255)
                $errors->add("staff->languageCode",
                    new ActionMessage("errors.maxlength", true, array(
                        new ActionMessage("prompt.language", true),
                        new ActionMessage("255", false))));


            global $ALLOWABLE_IMAGES;
            if(!is_null($this->getFormImage()) && isset($this->formImage["size"]) && $this->formImage["size"] > 0 && !array_key_exists($this->formImage["type"], $ALLOWABLE_IMAGES))
                $errors->add("formImage",
                    new ActionMessage("errors.image", true, new ActionMessage("prompt.image", true)));

            return $errors;
        }

    }
?>